<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: recover_cart_sales.php 105 2013-09-21 12:07:02Z phone.mueller@googlemail.com $
* 	Letzter StAND:			$Revision: 105 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:07:02 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
require_once(DIR_WS_CLASSES.'currencies.php');
require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.phpmailer.php');
require_once(DIR_FS_INC.'inc.image_button.php');
require_once(DIR_FS_INC.'inc.php_mail.php');
require_once(DIR_FS_INC.'inc.calculate_tax.php');

$currencies = new currencies();

if (isset($_GET['action']) && $_GET['action']=='complete') {
    $cID = (int)$_GET['customer_id'];
    
	require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.price.php');
    $status = $db->db_query("SELECT 
    							c.customers_status, 
    							cs.customers_status_name,  
    							cs.customers_status_image, 
    							cs.customers_status_ot_discount_flag, 
    							cs.customers_status_ot_discount 
    						FROM 
    							".TABLE_CUSTOMERS." c, 
    							".TABLE_CUSTOMERS_STATUS." cs 
    						WHERE 
    							c.customers_status = cs.customers_status_id 
    						AND 
    							c.customers_id = '".$cID."' 
    						AND 
    							cs.language_id = ".(int)$_SESSION['languages_id']);
								
    $price = new price(DEFAULT_CURRENCY, $status->fields['customers_status']);
	
    $_SESSION['saved_cart'] = $_SESSION['cart'];
    
    require_once(DIR_WS_CLASSES.'class.cart.php');
	
	$_SESSION['cart'] = new cart();
    $_SESSION['cart']->restore_contents($cID);

    require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.product.php');
    require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.main.php');
    $main = new main();
    
    $_SESSION['payment'] = DEFAULT_RCS_PAYMENT;
    require_once(DIR_WS_CLASSES.'payment.php');
    $payment_modules = new payment($_SESSION['payment']);
	
	$_SESSION['shipping'] = DEFAULT_RCS_SHIPPING;
    require_once(DIR_WS_CLASSES.'shipping.php');
    $shipping_modules = new shipping($_SESSION['shipping']);

    require (DIR_WS_CLASSES.'order_rcs.php');
    $order = new order($cID);

    if ($order->billing['country']['iso_code_2'] != '' && $order->delivery['country']['iso_code_2'] == '')
        $_SESSION['delivery_zone'] = $order->billing['country']['iso_code_2'];
    else
        $_SESSION['delivery_zone'] = $order->delivery['country']['iso_code_2'];

    $shipping_num_boxes = 1;
    
    list ($module, $method) = explode('_', $_SESSION['shipping']);
    if (is_object($$module)) {
        $quote = $shipping_modules->quote($method, $module);
        if (isset ($quote['error'])) {
            unset($_SESSION['shipping']);
        } else {
            if ((isset ($quote[0]['methods'][0]['title'])) && (isset ($quote[0]['methods'][0]['cost']))) {
                $_SESSION['shipping'] = array ('id' => $_SESSION['shipping'], 'title' => (($free_shipping == true) ? $quote[0]['methods'][0]['title'] : $quote[0]['module'].' ('.$quote[0]['methods'][0]['title'].')'), 'cost' => $quote[0]['methods'][0]['cost']);
            }
        }
    } else {
        $shipping_modules = MODULE_SHIPPING_INSTALLED;
    }
    $order = new order($cID);

    require (DIR_WS_CLASSES.'order_total.php');
    $order_total_modules = new order_total();
    $order_totals = $order_total_modules->process();

    $tmp = false;
    $tmp_status = $order->info['order_status'];

    if ($status->fields['customers_status_ot_discount_flag'] == 1)
        $discount = $status->fields['customers_status_ot_discount'];
    else
        $discount = '0.00';

    $sql_data_array = array('customers_id' => $cID,
							'customers_name' => $order->customer['firstname'].' '.$order->customer['lastname'],
							'customers_firstname' => $order->customer['firstname'],
							'customers_lastname' => $order->customer['lastname'],
							'customers_cid' => $order->customer['csID'],
							'customers_vat_id' => '',
							'customers_company' => $order->customer['company'],
							'customers_status' => $status->fields['customers_status'],
							'customers_status_name' => $status->fields['customers_status_name'],
							'customers_status_image' => $status->fields['customers_status_image'],
							'customers_status_discount' => $discount,
							'customers_street_address' => $order->customer['street_address'],
							'customers_suburb' => $order->customer['suburb'],
							'customers_city' => $order->customer['city'],
							'customers_postcode' => $order->customer['postcode'],
							'customers_state' => $order->customer['state'],
							'customers_country' => $order->customer['country']['title'],
							'customers_telephone' => $order->customer['telephone'],
							'customers_email_address' => $order->customer['email_address'],
							'customers_address_format_id' => $order->customer['format_id'],
							'delivery_name' => $order->delivery['firstname'].' '.$order->delivery['lastname'],
							'delivery_firstname' => $order->delivery['firstname'],
							'delivery_lastname' => $order->delivery['lastname'],
							'delivery_company' => $order->delivery['company'],
							'delivery_street_address' => $order->delivery['street_address'],
							'delivery_suburb' => $order->delivery['suburb'],
							'delivery_city' => $order->delivery['city'],
							'delivery_postcode' => $order->delivery['postcode'],
							'delivery_state' => $order->delivery['state'],
							'delivery_country' => $order->delivery['country']['title'],
							'delivery_country_iso_code_2' => $order->delivery['country']['iso_code_2'],
							'delivery_address_format_id' => $order->delivery['format_id'],
							'billing_name' => $order->billing['firstname'].' '.$order->billing['lastname'],
							'billing_firstname' => $order->billing['firstname'],
							'billing_lastname' => $order->billing['lastname'],
							'billing_company' => $order->billing['company'],
							'billing_street_address' => $order->billing['street_address'],
							'billing_suburb' => $order->billing['suburb'],
							'billing_city' => $order->billing['city'],
							'billing_postcode' => $order->billing['postcode'],
							'billing_state' => $order->billing['state'],
							'billing_country' => $order->billing['country']['title'],
							'billing_country_iso_code_2' => $order->billing['country']['iso_code_2'],
							'billing_address_format_id' => $order->billing['format_id'],
							'payment_method' => $order->info['payment_method'],
							'payment_class' => $order->info['payment_class'],
							'shipping_method' => $order->info['shipping_method'],
							'shipping_class' => $order->info['shipping_class'],
							'date_purchased' => 'NOW()',
							'orders_status' => $tmp_status,
							'currency' => $order->info['currency'],
							'currency_value' => $order->info['currency_value'],
							'customers_ip' => $customers_ip,
							'language' => $_SESSION['language'],
							'comments' => $order->info['comments']);

    $db->db_perform(TABLE_ORDERS, $sql_data_array);
    $insert_id = $db->db_insert_id();
    $_SESSION['tmp_oID'] = $insert_id;
    for ($i = 0, $n = sizeof($order_totals); $i < $n; $i ++) {
        $sql_data_array = array('orders_id' => $insert_id, 
        						'title' => $order_totals[$i]['title'], 
        						'text' => $order_totals[$i]['text'], 
        						'value' => $order_totals[$i]['value'], 
        						'class' => $order_totals[$i]['code'], 
        						'sort_order' => $order_totals[$i]['sort_order']);
        $db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);
    }

    $customer_notification = (SEND_EMAILS == 'true') ? '1' : '0';
    $sql_data_array = array('orders_id' => $insert_id, 
    						'orders_status_id' => $order->info['order_status'], 
    						'date_added' => 'NOW()', 
    						'customer_notified' => $customer_notification, 
    						'comments' => $order->info['comments']);
    $db->db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);

    $products_ordered = '';
    $products_ordered_html = '';
    $subtotal = 0;
    $total_tax = 0;

    for($i = 0, $n = sizeof($order->products); $i < $n; $i ++) {
        if(STOCK_LIMITED == 'true') {
            if(DOWNLOAD_ENABLED == 'true') {
				$stock_query_raw = "	SELECT 
											products_quantity, 
											pad.products_attributes_filename
										FROM 
											".TABLE_PRODUCTS." p
										LEFT JOIN 
											".TABLE_PRODUCTS_ATTRIBUTES." pa
										ON 
											p.products_id=pa.products_id
										LEFT JOIN 
											".TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD." pad
										ON 
											pa.products_attributes_id=pad.products_attributes_id
										WHERE 
											p.products_id = '".get_prid($order->products[$i]['id'])."'";
											
                $products_attributes = $order->products[$i]['attributes'];
                if (is_array($products_attributes))
                    $stock_query_raw .= " AND pa.options_id = '".$products_attributes[0]['option_id']."' AND pa.options_values_id = '".$products_attributes[0]['value_id']."'";
                
                $stock_values = $db->db_query($stock_query_raw);
                
            } else {
                $stock_values = $db->db_query("	SELECT 
													products_quantity 
												FROM 
													".TABLE_PRODUCTS." 
												WHERE 
													products_id = '".get_prid($order->products[$i]['id'])."'");
            }
            if ($stock_values->_numOfRows > 0) {
                if ((DOWNLOAD_ENABLED != 'true') || (!$stock_values->fields['products_attributes_filename']))
                    $stock_left = $stock_values->fields['products_quantity'] - $order->products[$i]['qty'];
                else
                    $stock_left = $stock_values->fields['products_quantity'];
                
                $db->db_query("	UPDATE 
									".TABLE_PRODUCTS." 
								SET 
									products_quantity = '".$stock_left."' 
								WHERE 
									products_id = '".get_prid($order->products[$i]['id'])."'");
            }
        }

        $db->db_query("	UPDATE 
							".TABLE_PRODUCTS." 
						SET 
							products_ordered = products_ordered + ".sprintf('%d', $order->products[$i]['qty'])." 
						WHERE 
							products_id = '".get_prid($order->products[$i]['id'])."'");

        $sql_data_array = array('orders_id' => $insert_id, 
        						'products_id' => get_prid($order->products[$i]['id']),
        						'products_ean' => $order->products[$i]['ean'], 
        						'products_model' => $order->products[$i]['model'], 
        						'products_name' => $order->products[$i]['name'],
        						'products_shipping_time'=>$order->products[$i]['shipping_time'], 
        						'products_price' => $order->products[$i]['price'], 
        						'final_price' => $order->products[$i]['final_price'], 
        						'products_tax' => $order->products[$i]['tax'], 
        						'products_discount_made' => $order->products[$i]['discount_allowed'], 
        						'products_quantity' => $order->products[$i]['qty'], 
        						'allow_tax' => $_SESSION['customers_status']['customers_status_show_price_tax']);

        $db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array);
        $order_products_id = $db->db_insert_id();

        $spq = $db->db_query("	SELECT 
									products_id, 
									specials_quantity 
								FROM 
									".TABLE_SPECIALS." 
								WHERE 
									products_id = '".get_prid($order->products[$i]['id'])."' ");
									
        if ($spq->_numOfRows) {

            $new_sp_quantity = ($spq->fields['specials_quantity'] - $order->products[$i]['qty']);

            if ($new_sp_quantity >= 1) {
                $db->db_query("	UPDATE 
									".TABLE_SPECIALS." 
								SET 
									specials_quantity = '".$new_sp_quantity."' 
								WHERE 
									products_id = '".get_prid($order->products[$i]['id'])."' ");
            } else {
                $db->db_query("	UPDATE 
									".TABLE_SPECIALS." 
								SET 
									status = '0', 
									specials_quantity = '".$new_sp_quantity."' 
								WHERE 
									products_id = '".get_prid($order->products[$i]['id'])."' ");
            }
        }

        $order_total_modules->update_credit_account($i);
        $attributes_exist = '0';
        $products_ordered_attributes = '';
        if (isset ($order->products[$i]['attributes'])) {
            $attributes_exist = '1';
            for ($j = 0, $n2 = sizeof($order->products[$i]['attributes']); $j < $n2; $j ++) {
                if (DOWNLOAD_ENABLED == 'true') {
                    $attributes_VALUES = $db->db_query("SELECT 
															popt.products_options_name,
															poval.products_options_values_name,
															pa.options_values_price,
															pa.price_prefix,
															pad.products_attributes_maxdays,
															pad.products_attributes_maxcount,
															pad.products_attributes_filename
														FROM 
															".TABLE_PRODUCTS_OPTIONS." popt,
															".TABLE_PRODUCTS_OPTIONS_VALUES." poval,
															".TABLE_PRODUCTS_ATTRIBUTES." pa
														LEFT JOIN 
															".TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD." pad
														ON 
															pa.products_attributes_id=pad.products_attributes_id
														WHERE 
															pa.products_id = '".$order->products[$i]['id']."'
														AND 
															pa.options_id = '".$order->products[$i]['attributes'][$j]['option_id']."'
														AND 
															pa.options_id = popt.products_options_id
														AND 
															pa.options_values_id = '".$order->products[$i]['attributes'][$j]['value_id']."'
														AND 
															pa.options_values_id = poval.products_options_values_id
														AND 
															popt.language_id = ".(int)$_SESSION['languages_id']."
														AND 
															poval.language_id = ".(int)$_SESSION['languages_id']);
                } else {
                    $attributes_VALUES = $db->db_query("SELECT 
															popt.products_options_name,
															poval.products_options_values_name,
															pa.options_values_price,
															pa.price_prefix
														FROM 
															".TABLE_PRODUCTS_OPTIONS." popt,
															".TABLE_PRODUCTS_OPTIONS_VALUES." poval,
															".TABLE_PRODUCTS_ATTRIBUTES." pa
														WHERE 
															pa.products_id = '".$order->products[$i]['id']."'
														AND 
															pa.options_id = '".$order->products[$i]['attributes'][$j]['option_id']."'
														AND 
															pa.options_id = popt.products_options_id
														AND 
															pa.options_values_id = '".$order->products[$i]['attributes'][$j]['value_id']."'
														AND 
															pa.options_values_id = poval.products_options_values_id
														AND 
															popt.language_id = ".(int)$_SESSION['languages_id']."
														AND 
															poval.language_id = ".(int)$_SESSION['languages_id']);
                }

				$db->db_query("	UPDATE 
									".TABLE_PRODUCTS_ATTRIBUTES."
								SET 
									attributes_stock=attributes_stock - '".$order->products[$i]['qty']."'
								WHERE 
									products_id='".$order->products[$i]['id']."'
								AND 
									options_values_id='".$order->products[$i]['attributes'][$j]['value_id']."'
								AND 
									options_id='".$order->products[$i]['attributes'][$j]['option_id']."'");

                $sql_data_array = array('orders_id' => $insert_id, 
                						'orders_products_id' => $order_products_id, 
                						'products_options' => $attributes_VALUES->fields['products_options_name'], 
                						'products_options_values' => $attributes_VALUES->fields['products_options_values_name'], 
                						'options_values_price' => $attributes_VALUES->fields['options_values_price'], 
                						'price_prefix' => $attributes_VALUES->fields['price_prefix']);
                $db->db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array);

                if ((DOWNLOAD_ENABLED == 'true') && isset ($attributes_VALUES->fields['products_attributes_filename']) && not_null($attributes_VALUES->fields['products_attributes_filename'])) {
                    $sql_data_array = array('orders_id' => $insert_id, 
                    						'orders_products_id' => $order_products_id, 
                    						'orders_products_filename' => $attributes_VALUES->fields['products_attributes_filename'], 
                    						'download_maxdays' => $attributes_VALUES->fields['products_attributes_maxdays'], 
                    						'download_count' => $attributes_VALUES->fields['products_attributes_maxcount']);
                    $db->db_perform(TABLE_ORDERS_PRODUCTS_DOWNLOAD, $sql_data_array);
                }
            }
        }
        
        $total_weight += ($order->products[$i]['qty'] * $order->products[$i]['weight']);
        $total_tax += calculate_tax($total_products_price, $products_tax) * $order->products[$i]['qty'];
        $total_cost += $total_products_price;
    }
    if (RCS_DELETE_COMPLETED_ORDERS == 'true') {
        $db->db_query("DELETE FROM ".TABLE_CUSTOMERS_BASKET." WHERE customers_id=".$cID);
        $db->db_query("DELETE FROM ".TABLE_CUSTOMERS_BASKET_ATTRIBUTES." WHERE customers_id=".$cID);
        $db->db_query("DELETE FROM ".TABLE_SCART." WHERE customers_id=".$cID);
    }
    $_SESSION['cart'] = $_SESSION['saved_cart'];
    redirect(href_link(FILENAME_ORDERS, "oID=".$insert_id."&action=edit"));
}

if ($_GET['action']=='delete') {
    $cID = (int)$_GET['customer_id'];
    $db->db_query("DELETE FROM ".TABLE_CUSTOMERS_BASKET." WHERE customers_id = ".$cID);
    $db->db_query("DELETE FROM ".TABLE_CUSTOMERS_BASKET_ATTRIBUTES." WHERE customers_id = ".$cID);
    $db->db_query("DELETE FROM ".TABLE_SCART." WHERE customers_id = ".$cID);

    redirect(href_link(FILENAME_RECOVER_CART_SALES, 'delete=1&customer_id = '. $_GET['customer_id'].'&tdate='.$_GET['tdate']));
}

if ($_GET['delete'])
    $message_stack->add(MESSAGE_STACK_CUSTOMER_ID.$_GET['customer_id'].MESSAGE_STACK_DELETE_SUCCESS, 'success');

$tdate = $_POST['tdate'];
if($tdate == '') 
	$tdate = RCS_BASE_DAYS;

require(DIR_WS_INCLUDES.'metatag.php');
?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php');
function seadate($day) {
    $rawtime = strtotime("-".$day." days");
    $ndate = date("Ymd", $rawtime);
    return $ndate;
}

function cart_date_short($raw_date) {
    if ( ($raw_date == '00000000') || ($raw_date == '') ) return false;

    $year = substr($raw_date, 0, 4);
    $month = (int)substr($raw_date, 4, 2);
    $day = (int)substr($raw_date, 6, 2);

    if (@date('Y', mktime(0, 0, 0, $month, $day, $year)) == $year) {
        return date(DATE_FORMAT, mktime(0, 0, 0, $month, $day, $year));
    } else {
        return preg_replace("/2037/i", $year, date(DATE_FORMAT, mktime(0, 0, 0, $month, $day, 2037)));
    }
}

function _GetCustomerSessions() {
	global $db;
    $cust_ses_ids = array();

    if( RCS_CHECK_SESSIONS == 'true' ) {
        if (STORE_SESSIONS == 'mysql') {
            $ses = $db->db_query("SELECT value FROM ".TABLE_SESSIONS." WHERE 1");
            while (!$ses->EOF) {
                if( preg_match("/customer_id[^\"]*\"([0-9]*)\"/i", $ses->fields['value'], $custval))
                $cust_ses_ids[] = $custval[1];
                $ses->MoveNext();
            }
            
        } else {
            if( $handle = opendir( session_save_path())){
                while (false !== ($file = readdir($handle))) {
                    if ($file != "." && $file != "..") {
                        $file = session_save_path().'/'.$file;    // create full path to file!
                        if($fp = fopen( $file, 'r' )) {
                            $val = fread( $fp, filesize($file));
                            fclose($fp);
                            if(preg_match("/customer_id[^\"]*\"([0-9]*)\"/i", $val, $custval))
                            	$cust_ses_ids[] = $custval[1];
                        }
                    }
                }
                closedir( $handle );
            }
        }
    }
    return $cust_ses_ids;
}
?>
<div id="wrapper">
<table class="outerTable" cellpadding="0" cellspacing="0">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top" class="columnLeft2">
		<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
	</td>
    <td class="boxCenter" valign="top">
    <?php if (count($_POST['custid']) > 0 ) {  ?>
	    <table class="table_pageHeading" width="100%">
            <tr>
              <td class="pageHeading" align="left" colspan="2" width="50%"><? echo HEADING_TITLE; ?> </td>
              <td class="pageHeading" align="left" colspan="4" width="50%"><? echo HEADING_EMAIL_SENT; ?> </td>
            </tr>
	     </table>
	     <table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
	            <tr class="dataTableHeadingRow">
	              <td class="dataTableHeadingContent" align="left" colspan="1" width="15%" nowrap><?php echo TABLE_HEADING_CUSTOMER; ?></td>
	              <td class="dataTableHeadingContent" align="left" colspan="1" width="30%" nowrap>&nbsp;</td>
	              <td class="dataTableHeadingContent" align="left" colspan="1" width="25%" nowrap>&nbsp;</td>
	              <td class="dataTableHeadingContent" align="left" colspan="1" width="10%" nowrap>&nbsp;</td>
	              <td class="dataTableHeadingContent" align="left" colspan="1" width="10%" nowrap>&nbsp;</td>
	              <td class="dataTableHeadingContent" align="left" colspan="1" width="10%" nowrap>&nbsp;</td>
	            </tr>
	            <tr class="dataTableHeadingRow">
	              <td class="dataTableHeadingContent" align="left" colspan="1" width="15%" nowrap><?php echo TABLE_HEADING_MODEL; ?></td>
	              <td class="dataTableHeadingContent" align="left" colspan="2" width="55%" nowrap><?php echo TABLE_HEADING_DESCRIPTION; ?></td>
	              <td class="dataTableHeadingContent" align="center" colspan="1" width="10%" nowrap><?php echo TABLE_HEADING_QUANTY; ?></td>
	              <td class="dataTableHeadingContent" align="right" colspan="1" width="10%" nowrap><?php echo TABLE_HEADING_PRICE; ?></td>
	              <td class="dataTableHeadingContent" align="right" colspan="1" width="10%" nowrap><?php echo TABLE_HEADING_TOTAL; ?></td>
	            </tr>
	     </table>
<?php

foreach ($_POST['custid'] as $cid) {
    $quantity = array();
    $products_data = array();
    $quantityResult = $db->db_query("SELECT products_id pid, customers_basket_quantity qty FROM ".TABLE_CUSTOMERS_BASKET." WHERE customers_id=".$cid);
    while (!$quantityResult->EOF) {
        $quantity[(int)$quantityResult->fields['pid']] += $quantityResult->fields['qty'];
		$quantityResult->MoveNext();
    }
    $inrec = $db->db_query("SELECT 
								cb.products_id pid,
								cb.customers_basket_quantity qty,
								cb.customers_basket_date_added bdate,
								cb.checkout_site site,
								cb.language,
								cus.customers_firstname fname,
								cus.customers_lastname lname,
								cus.customers_gender,
								cus.customers_email_address email,
								co.countries_iso_code_2 iso
							FROM      
								".TABLE_CUSTOMERS_BASKET." cb,
								".TABLE_CUSTOMERS." cus,
								".TABLE_ADDRESS_BOOK." ab,
								".TABLE_COUNTRIES." co
							WHERE     
								cb.customers_id = cus.customers_id
							AND       
								cus.customers_id = '".$cid."'
							AND       
								cus.customers_default_address_id = ab.address_book_id
							AND       
								co.countries_id = ab.entry_country_id
							ORDER BY  
								cb.customers_basket_date_added DESC ");

    $knt = $inrec->_numOfRows;
    $td = 1;
    for ($i = 0; $i < $knt; $i++) {
        $aprice = 0;
        if ($lastcid != $cid) {
            if ($lastcid != "") {
                $text_total = RCS_SHOW_BRUTTO_PRICE == 'true'?TABLE_CART_TOTAL_BRUTTO:TABLE_CART_TOTAL;

                $cline .= "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"dataTable\">
              <tr>
                 <td class='dataTableContent' align='right' colspan='6' nowrap><b>".$text_total."</b>".$currencies->format($tprice)."</td>
              </tr>
              <tr>
                 <td colspan='6' align='right'><a class=\"button\" href=".href_link(FILENAME_RECOVER_CART_SALES, "action=delete&customer_id=".$cid."&tdate=".$tdate).">".BUTTON_DELETE."</a></td>
              </tr>";
                echo $cline;
            }
            $cline = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"dataTable\">
						<tr>
							<td class='dataTableContent' align='left' colspan='6' nowrap><a href='".href_link(FILENAME_CUSTOMERS, 'search='.$inrec->fields['lname'], 'NONSSL')."'>".$inrec->fields['fname']." ".$inrec->fields['lname']."</a>".$customer."
							</td>
						</tr>";
            $tprice = 0;
        }
        $lastcid = $cid;

        // get the shopping cart
        $inrec2 = $db->db_query("	SELECT  
										p.products_price price,
										p.products_model model,
										p.products_tax_class_id tax,
										p.products_image image,
										pd.products_name name
									FROM    
										".TABLE_PRODUCTS." p,
										".TABLE_PRODUCTS_DESCRIPTION." pd
									WHERE   
										p.products_id = '".$inrec->fields['pid']."' AND
										pd.products_id = p.products_id AND
										pd.language_id = ".(int)$_SESSION['languages_id'] );

        $sprice = get_products_special_price( $inrec->fields['pid'], $cid, ($inrec->fields['qty'] < $quantity[(int)$inrec->fields['pid']]?$quantity[(int)$inrec->fields['pid']]:$inrec->fields['qty']));
        // BEGIN OF ATTRIBUTE DB CODE
        $prodAttribs = ''; // DO NOT DELETE
        if (RCS_SHOW_ATTRIBUTES == 'true') {
            $attribrecs = $db->db_query("	SELECT 
												cba.products_id pid,
												po.products_options_name poname,
												pov.products_options_values_name povname,
												pa.options_values_price price
											FROM 
												".TABLE_CUSTOMERS_BASKET_ATTRIBUTES." cba,
												".TABLE_PRODUCTS_OPTIONS." po,
												".TABLE_PRODUCTS_OPTIONS_VALUES." pov,
												".TABLE_PRODUCTS_ATTRIBUTES." pa
											WHERE 
												cba.products_id = '".$inrec->fields['pid']."'
											AND 
												cba.customers_id = ".$cid."
											AND 
												po.products_options_id = cba.products_options_id
											AND 
												pov.products_options_values_id = cba.products_options_value_id
											AND 
												pa.products_id = ".(int)$inrec->fields['pid']."
											AND 
												pa.options_id = cba.products_options_id
											AND 
												pa.options_values_id = cba.products_options_value_id
											AND 
												po.language_id = ".(int)$_SESSION['languages_id']."
											AND 
												pov.language_id = ".(int)$_SESSION['languages_id']);
            $hasAttributes = false;

            if ($attribrecs->_numOfRows) {
                $hasAttributes = true;
                $prodAttribs = '<br>';
                while (!$attribrecs->EOF) {
                    $prodAttribs .= '<small><em> - '.$attribrecs->fields['poname'].' '.$attribrecs->fields['povname'].'</em></small><br >';
                    $aprice += $attribrecs->fields['price'];
					$attribrecs->MoveNext();
				}
            }
        }
        if( $sprice == 0 ) 
        	$sprice = $inrec2->fields['price'];
        $sprice += $aprice;
        if (RCS_SHOW_BRUTTO_PRICE == 'true') {
            $tax = get_tax_rate($inrec2->fields['tax']);
            $sprice = add_tax($sprice, $tax);
        }

        $tprice = $tprice + ($inrec->fields['qty'] * $sprice);
        $pprice_formated  = $currencies->format($sprice);
        $tpprice_formated = $currencies->format(($inrec->fields['qty'] * $sprice));

        $cline .= "<tr class='".$f."'>
                    <td class='dataTableContent' align='left'   width='15%' nowrap>".($inrec2->fields['model']?$inrec2->fields['model']:'&nbsp;')."</td>
                    <td class='dataTableContent' align='left'  colspan='2' width='55%'><a href='".href_link(FILENAME_CATEGORIES, 'action=new_product&pID='.$inrec->fields['pid'], 'NONSSL')."'>".$inrec2->fields['name']."</a></td>
                    <td class='dataTableContent' align='center' width='10%' nowrap>".$inrec->fields['qty']."</td>
                    <td class='dataTableContent' align='right'  width='10%' nowrap>".$pprice_formated."</td>
                    <td class='dataTableContent' align='right'  width='10%' nowrap>".$tpprice_formated."</td>
                 </tr>";
                 
        $products_data[$i] = array('QUANTITY' => $inrec->fields['qty'],
									'NAME' => $inrec2->fields['name'],
									'LINK' => catalog_href_link(FILENAME_CATALOG_PRODUCT_INFO, 'products_id='.$inrec->fields['pid']),
									'IMAGE' => HTTP_SERVER.DIR_WS_CATALOG_INFO_IMAGES.$inrec2->fields['image']);
        $td++;
    }

    $cline .= "</td></tr>";

    if ($inrec->fields['language'] == null) {
        switch($inrec->fields['iso']) {
            case 'DE':
            case 'AT':
            case 'CH':
                $inrec->fields['language'] = 'german';
                break;
                
            default:
                $inrec->fields['language'] = 'english';
        }
    }

    $cquery = $db->db_query("SELECT * FROM ".TABLE_ORDERS." WHERE customers_id = '".$cid."'" );

    $smarty = new Smarty();
    $smarty->assign('language', $inrec->fields['language']);

    $smarty->assign('products_data', $products_data);
    $smarty->assign('LOGIN', catalog_href_link(FILENAME_CATALOG_LOGIN, '', 'SSL'));

    if (RCS_EMAIL_FRIENDLY == 'true') {
        $smarty->assign('GENDER', $inrec->fields['customers_gender']);
        $smarty->assign('FIRSTNAME', $inrec->fields['fname']);
        $smarty->assign('LASTNAME', $inrec->fields['lname']);
    } else
        $smarty->assign('GENDER', false);

    if ($cquery->_numOfRows < 1)
        $smarty->assign('NEW', true);
    else
        $smarty->assign('NEW', false);

    $smarty->assign('STORE_LINK', catalog_href_link('', ''));
    $smarty->assign('STORE_NAME', STORE_NAME);

    $smarty->assign('MESSAGE', $_POST['message']);

    $outEmailAddr = '"'.$custname.'" <'.$inrec->fields['email'].'>';
    if( not_null(RCS_EMAIL_COPIES_TO) )
    	$outEmailAddr .= ', '.RCS_EMAIL_COPIES_TO;

	$smarty->caching = false;
	require_once (DIR_FS_INC.'inc.get_mail_body.php');
	$html_mail = $smarty->fetch('html:recover_cart_sales');
	$html_mail .= $signatur_html;
	$txt_mail = $smarty->fetch('txt:recover_cart_sales');
	$txt_mail .= $signatur_text;
	require_once (DIR_FS_INC.'inc.get_mail_data.php');
	$mail_data = get_mail_data('recover_cart_sales');

	$recover_cart_sales_subject = str_replace('{$nr}', $insert_id, $mail_data['EMAIL_SUBJECT']);
	$recover_cart_sales_subject = str_replace('{$date}', strftime(DATE_FORMAT_LONG), $recover_cart_sales_subject);
	$recover_cart_sales_subject = str_replace('{$lastname}', $order->customer['lastname'], $recover_cart_sales_subject);
	$recover_cart_sales_subject = str_replace('{$firstname}', $order->customer['firstname'], $recover_cart_sales_subject);
	$recover_cart_sales_subject = str_replace('{$shop_besitzer}', STORE_OWNER, $recover_cart_sales_subject);
	$recover_cart_sales_subject = str_replace('{$shop_name}', STORE_NAME, $recover_cart_sales_subject);

	if ($inrec->fields['email'] != '') {
		php_mail($mail_data['EMAIL_ADDRESS'],
					$FROM_name,
					$inrec->fields['email'],
					$custname,
					'',
					$mail_data['EMAIL_REPLAY_ADDRESS'],
					$FROM_name,
					'',
					'',
					$recover_cart_sales_subject,
					$html_mail,
					$txt_mail);
    }
    
    $donequery = $db->db_query("SELECT * FROM ".TABLE_SCART." WHERE customers_id = '".$cid."'");
    if ($donequery->_numOfRows == 0)
		$db->db_query("INSERT INTO ".TABLE_SCART." (customers_id, dateadded, datemodified ) VALUES ('".$cid."', '".seadate('0')."', '".seadate('0')."')");
    else
		$db->db_query("UPDATE ".TABLE_SCART." SET datemodified = '".seadate('0')."' WHERE customers_id = ".$cid );

    echo $cline;
    $cline = "";
    $text_total = RCS_SHOW_BRUTTO_PRICE == 'true'?TABLE_CART_TOTAL_BRUTTO:TABLE_CART_TOTAL;
}
echo "<tr><td colspan=8 align='right' class='dataTableContent'><b>".$text_total."</b>".$currencies->format($tprice)."</td> </tr>";
echo "<tr><td colspan=6 align='right'><a class=\"button\" href=".href_link(FILENAME_RECOVER_CART_SALES, "action=delete&customer_id=".$cid."&tdate=".$tdate).">".BUTTON_DELETE."</a></td>  </tr>\n";
echo "<tr><td colspan=6 align=center><a href=".$PHP_SELF.">".TEXT_RETURN."</a></td></tr>";
} else {
?>
        <!-- REPORT TABLE BEGIN //-->
        <table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
            <tr>
              <td class="pageHeading" align="left" width="50%" colspan="4"><?php echo HEADING_TITLE; ?></td>
              <td class="pageHeading" align="right" width="50%" colspan="4">
              	<?php echo draw_form('form', FILENAME_RECOVER_CART_SALES); ?>
                  <table align="right" width="100%">
                    <tr class="dataTableContent" align="right">
                      <td><?php echo DAYS_FIELD_PREFIX; ?><input type=text size=4 width=4 value=<?php echo $tdate; ?> name=tdate><?php echo DAYS_FIELD_POSTFIX; ?><input class="button" type=submit value="<?php echo DAYS_FIELD_BUTTON; ?>"></td>
                    </tr>
                  </table>
                </form>
              </td>
            </tr>
          </table>
			<?php echo draw_form('form', FILENAME_RECOVER_CART_SALES); ?>
		<table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
            <tr class="dataTableHeadingRow">
              <th class="dataTableHeadingContent" align="left" colspan="2" width="10%" nowrap><?php echo TABLE_HEADING_CONTACT; ?></th>
              <th class="dataTableHeadingContent" align="left" colspan="1" width="15%" nowrap><?php echo TABLE_HEADING_DATE; ?></th>
              <th class="dataTableHeadingContent" align="left" colspan="1" width="30%" nowrap><?php echo TABLE_HEADING_CUSTOMER; ?></th>
              <th class="dataTableHeadingContent" align="left" colspan="1" width="20%" nowrap><?php echo TABLE_HEADING_EMAIL; ?></th>
              <th class="dataTableHeadingContent" align="left" colspan="1" width="10%" nowrap><?php echo TABLE_HEADING_STOPPED; ?></th>
              <th class="dataTableHeadingContent" align="left" colspan="2" width="15%" nowrap><?php echo TABLE_HEADING_PHONE; ?></th>
            </tr>
            <tr class="dataTableHeadingRow">
              <th class="dataTableHeadingContent" align="left"   colspan="2"  width="10%" nowrap><?php echo TABLE_HEADING_OUT_DATE ?> </th>
              <th class="dataTableHeadingContent" align="left"   colspan="1"  width="15%" nowrap><?php echo TABLE_HEADING_MODEL; ?></th>
              <th class="dataTableHeadingContent" align="left"   colspan="2" width="55%" nowrap><?php echo TABLE_HEADING_DESCRIPTION; ?></th>
              <th class="dataTableHeadingContent" align="center" colspan="1" width="5%" nowrap> <?php echo TABLE_HEADING_QUANTY; ?></th>
              <th class="dataTableHeadingContent" align="right"  colspan="1"  width="5%" nowrap><?php echo TABLE_HEADING_PRICE; ?></th>
              <th class="dataTableHeadingContent" align="right"  colspan="1" width="10%" nowrap><?php echo TABLE_HEADING_TOTAL; ?></th>
            </tr>
			<?php
			if ($cust_ses_ids = _GetCustomerSessions()) {
			    $cust_sql = " AND customers_id not in ('".implode(", ", $cust_ses_ids)."') ";
			    //echo "-".$cust_sql;
			}
			$ndate = seadate($tdate);
			$query1Res = $db->db_query("	SELECT 
												customers_id, 
												MAX(customers_basket_date_added) as last 
											FROM 
												".TABLE_CUSTOMERS_BASKET." 
											WHERE
												customers_basket_date_added>='".$ndate."' ".$cust_sql." 
											GROUP BY 
												customers_id 
											ORDER BY 
												last DESC, 
												customers_id");

$results = 0;
$curcus = "";
$tprice = 0;
$totalAll = 0;
$first_line = true;
$final_line = false;
$skip = false;
$knt = $query1Res->_numOfRows;
$i=1;
while (!$query1Res->EOF) {
	$cline .= '<table width="100%" cellspacing="0" cellpadding="0" class="dataTable">';
    $quantity = array();
	
    $quantityResult = $db->db_query("	SELECT 
											products_id pid, 
											customers_basket_quantity qty 
										FROM 
											".TABLE_CUSTOMERS_BASKET." 
										WHERE 
											customers_id=".$query1Res->fields['customers_id']);
										
    while (!$quantityResult->EOF) {
        $quantity[(int)$quantityResult->fields['pid']] += $quantityResult->fields['qty'];
		$quantityResult->MoveNext();
    }
	
    $data = $db->db_query("	SELECT 
								cb.customers_id cid,
								cb.products_id pid,
								cb.customers_basket_quantity qty,
								cb.customers_basket_date_added bdate,
								cb.checkout_site site,
								cus.customers_firstname fname,
								cus.customers_lastname lname,
								cus.customers_telephone phone,
								cus.customers_email_address email
							FROM  
								".TABLE_CUSTOMERS_BASKET." cb,
								".TABLE_CUSTOMERS." cus
							WHERE 
								cb.customers_id = cus.customers_id
							AND   
								cb.customers_id = ".$query1Res->fields['customers_id']."
							ORDER BY 
								cb.customers_basket_date_added DESC");

    while(!$data->EOF) {
        $inrec = $data;
        //reSET attributes price
        $aprice = 0;
        // If this is a new customer, create the appropriate HTML
        if ($curcus != $inrec->fields['cid']) {
            // output line
            $final_line = true;
            // SET new cline AND curcus
            $curcus = $inrec->fields['cid'];
            if ($curcus != "") {
                $tprice = 0;

                // change the color on those we have contacted add customer tag to customers
                $fcolor = RCS_UNCONTACTED_COLOR;
                $checked = 1;    // assume we'll send an email
                $new = 1;
                $skip = false;
                $sentdate = "";
                $beforeDate = RCS_CARTS_MATCH_ALL_DATES == 'true' ? '0' : $inrec->fields['bdate'];
                $customer = $inrec->fields['fname']." ".$inrec->fields['lname'];
                $status = "";

                $ttl = $db->db_query("SELECT * FROM ". TABLE_SCART ." WHERE customers_id = '".$curcus."'");
                $emailttl = seadate(RCS_EMAIL_TTL);

                if ($ttl->_numOfRows > 0) {
                    if( $ttl )
                    {
                        if( not_null($ttl->fields['datemodified']) )    // allow for older scarts that have no datemodified
							$ttldate = $ttl->fields['datemodified'];
                        else
							$ttldate = $ttl->fields['dateadded'];

                        if ($emailttl <= $ttldate) {
                            $sentdate = $ttldate;
                            $fcolor = RCS_CONTACTED_COLOR;
                            $checked = 0;
                            $new = 0;
                        }
                    }
                }

                // See if the customer has purchased FROM us before
                // Customers are identified by either their customer ID or name or email address
                // If the customer has an order with items that match the current order, assume order completed, bail on this entry!
                $orec = $db->db_query('	SELECT 
											orders_id, orders_status
										FROM 
											'.TABLE_ORDERS.'
										WHERE 
											(customers_id = '.(int)$curcus.'
										OR 
											customers_email_address like "'.$inrec->fields['email'] .'"
										OR 
											customers_name like "'.$inrec->fields['fname'].' '.$inrec->fields['lname'].'")
										AND 
											date_purchased >= "'.$beforeDate.'"' );
											
                if ($orec->_numOfRows){
                    // We have a matching order; assume current customer but not for this order
                    $customer = '<font color='.RCS_CURCUST_COLOR.'><b>'.$customer.'</b></font>';

                    // Now, look to see if one of the orders matches this current order's items
                    while(!$orec->EOF){
					
                        $srec = $db->db_query('	SELECT 
														products_id 
													FROM 
														'.TABLE_ORDERS_PRODUCTS.' 
													WHERE 
														orders_id = '.(int)$orec->fields['orders_id'].' 
													AND 
														products_id = '.(int)$inrec->fields['pid'] );
														
                        if($srec->_numOfRows  > 0 )
                        {
                            if( $orec->fields['orders_status'] > RCS_PENDING_SALE_STATUS )
								$checked = 0;

                            // OK, we have a matching order; see if we should just skip this or show the status
                            if( RCS_SKIP_MATCHED_CARTS == 'true' && !$checked ){
                                $skip = true;    // reSET flag & break us out of the while loop!
                                break;
                            }else{
                                // It's rare for the same customer to order the same item twice, so we probably have a matching order, show it
                                $fcolor = RCS_MATCHED_ORDER_COLOR;
                                $ccquery = $db->db_query("SELECT orders_status_name FROM ".TABLE_ORDERS_STATUS." WHERE language_id = ".(int)$_SESSION['languages_id']." AND orders_status_id = ".(int)$orec->fields['orders_status'] );

                                if($srec->_numOfRows) 
                                    $status = ' <a href="'.href_link(FILENAME_ORDERS, "oID=".$orec->fields['orders_id']."&action=edit"). '">['.$srec->fields['orders_status_name'].']</a>';
                                else 
                                    $status = ' ['. TEXT_CURRENT_CUSTOMER.']';
                                
                            }
                        }
					$orec->MoveNext();
                    }
                    if( $skip )
                    continue;    // got a matched cart, skip to next one
                }
                $sentInfo = TEXT_NOT_CONTACTED;

                if ($sentdate != '')
                $sentInfo = cart_date_short($sentdate);
                $site = $inrec->fields['site'] == 'confirm' ? TEXT_CONFIRM : ($inrec->fields['site'] == 'payment' ? TEXT_PAYMENT : ($inrec->fields['site'] == 'shipping' ? TEXT_SHIPPING : TEXT_CART));

                $cline = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"dataTable\">
                <tr bgcolor=".$fcolor.">
                <td class='dataTableContent' align='center' width='1%'>".draw_checkbox_field('custid[]', $curcus, RCS_AUTO_CHECK == 'true' ? $checked : 0)."</td>
                <td class='dataTableContent' align='left' width='9%' nowrap><b>".$sentInfo."</b></td>
                <td class='dataTableContent' align='left' width='15%' nowrap> ".cart_date_short($inrec->fields['bdate'])."</td>
                <td class='dataTableContent' align='left' width='30%' nowrap><a href='".href_link(FILENAME_CUSTOMERS, 'search='.$inrec->fields['lname'], 'NONSSL')."'>".$customer."</a>".$status."</td>
                <td class='dataTableContent' align='left' width='20%' nowrap><a href='".href_link('mail.php', 'selected_box=tools&customer='.$inrec->fields['email'])."'>".$inrec->fields['email']."</a></td>
                <td class='dataTableContent' align='left' width='10%' nowrap>".$site."</td>
                <td class='dataTableContent' align='left' colspan='2' width='15%' nowrap>".$inrec->fields['phone']."</td>
                </tr>";
            }
        }

        // We only have something to do for the product if the quantity SELECTed was not zero!
        if ($inrec->fields['qty'] != 0) {
            // Get the product information (name, price, etc)
            $inrec2 = $db->db_query("	SELECT 
											p.products_price price,
											p.products_model model,
											p.products_tax_class_id tax,
											pd.products_name name
										FROM    
											".TABLE_PRODUCTS." p,
											".TABLE_PRODUCTS_DESCRIPTION." pd
										WHERE   
											p.products_id = '".(int)$inrec->fields['pid']."'
										AND     
											pd.products_id = p.products_id
										AND     
											pd.language_id = ".(int)$_SESSION['languages_id'] );

            // Check to see if the product is on special, AND if so use that pricing
            $sprice = get_products_special_price( $inrec->fields['pid'], $inrec->fields['cid'], ($inrec->fields['qty'] < $quantity[(int)$inrec->fields['pid']]?$quantity[(int)$inrec->fields['pid']]:$inrec->fields['qty']));
            // BEGIN OF ATTRIBUTE DB CODE
            $prodAttribs = ''; // DO NOT DELETE

            if (RCS_SHOW_ATTRIBUTES == 'true') {
                $attribrecs = $db->db_query("	SELECT  
													cba.products_id pid,
													po.products_options_name poname,
													pov.products_options_values_name povname,
													pa.options_values_price price
												FROM    
													".TABLE_CUSTOMERS_BASKET_ATTRIBUTES." cba,
													".TABLE_PRODUCTS_OPTIONS." po,
													".TABLE_PRODUCTS_OPTIONS_VALUES." pov,
													".TABLE_PRODUCTS_ATTRIBUTES." pa
												WHERE   
													cba.products_id = '".$inrec->fields['pid']."'
												AND         
													cba.customers_id = ".$curcus."
												AND         
													po.products_options_id = cba.products_options_id
												AND         
													pov.products_options_values_id = cba.products_options_value_id
												AND     
													pa.products_id = ".(int)$inrec->fields['pid']."
												AND     
													pa.options_id = cba.products_options_id
												AND         
													pa.options_values_id = cba.products_options_value_id
												AND       
													po.language_id = ".(int)$_SESSION['languages_id']."
												AND         
													pov.language_id = ".(int)$_SESSION['languages_id']);
                $hasAttributes = false;

                if ($attribrecs->_numOfRows) {
                    $hasAttributes = true;
                    $prodAttribs = '<br>';
                    while (!$attribrecs->EOF) {
                        $prodAttribs .= '<small><em> - '.$attribrecs->fields['poname'].' '.$attribrecs->fields['povname'].'</em></small><br >';
                        $aprice += $attribrecs->fields['price'];
					$attribrecs->MoveNext();
                    }
                }
            }
            if( $sprice == 0 ) $sprice = $inrec2->fields['price'];
            $sprice += $aprice;
            if (RCS_SHOW_BRUTTO_PRICE == 'true') {
                $tax = get_tax_rate($inrec2->fields['tax']);
                $sprice = add_tax($sprice, $tax);
            }

            // END OF ATTRIBUTE DB CODE
            $tprice = $tprice + ($inrec->fields['qty'] * $sprice);
            $pprice_formated  = $currencies->format($sprice);
            $tpprice_formated = $currencies->format(($inrec->fields['qty'] * $sprice));
            if ($i % 2 == 0)
	    		$f = 'dataTableRow';
	    	else
	    		$f = '';
            $cline .= "<tr class=".$f.">
                    <td class='dataTableContent' align='left' vAlign='top' colspan='2' width='10%' nowrap>".($inrec->fields['bdate']<$ndate? " x":" &nbsp;")."</td>
                    <td class='dataTableContent' align='left' vAlign='top' width='15%' nowrap>".($inrec2->fields['model']?$inrec2->fields['model']:"&nbsp;")."</td>
                    <td class='dataTableContent' align='left' vAlign='top' colspan='2' width='55%'><a href='".href_link(FILENAME_CATEGORIES, 'action=new_product&pID='.$inrec->fields['pid'], 'NONSSL')."'><b>".$inrec2->fields['name']."</b></a>
                    ".$prodAttribs."
                    </td>
                    <td class='dataTableContent' align='center' vAlign='top' width='5%' nowrap>".$inrec->fields['qty']."x</td>
                    <td class='dataTableContent' align='right'  vAlign='top' width='5%' nowrap>".$pprice_formated."</td>
                    <td class='dataTableContent' align='right'  vAlign='top' width='10%' nowrap>".$tpprice_formated."</td>
                 </tr>";
        }
    $i++;
	$data->MoveNext();
    }
    if ($final_line) {
        $totalAll += $tprice;
        $text_total = RCS_SHOW_BRUTTO_PRICE == 'true'?TABLE_CART_TOTAL_BRUTTO:TABLE_CART_TOTAL;

        $cline .= "<table width='100%' cellpadding='5' cellspacing='5'>
                        <tr>
                          <td class='dataTableContent no_border' align='right' colspan='8'><b>".$text_total."</b>".$currencies->format($tprice)."</td>
                        </tr>
                        <tr>
                          <td class='no_border' align='right'><a class=\"button\" href=".href_link(FILENAME_RECOVER_CART_SALES,"action=delete&customer_id=$curcus&tdate=$tdate").">".BUTTON_DELETE ."</a><a class=\"button\" href=".href_link(FILENAME_RECOVER_CART_SALES,"action=complete&customer_id=$curcus&tdate=$tdate").">".BUTTON_COMPLETE ."</a></td>
                        </tr></table>\n";
        if (!$skip) {
            echo $cline;
        }
        $final_line = false;

    }$i++;
$query1Res->MoveNext();
}
// echo '</table>';
$totalAll_formated = $currencies->format($totalAll);
$text_total = RCS_SHOW_BRUTTO_PRICE == 'true' ? TABLE_GRAND_TOTAL_BRUTTO : TABLE_GRAND_TOTAL;
$cline = "<table width='100%'><tr><td class='dataTableContent' align='right'><hr align=right width=200><b>".$text_total."</b>".$totalAll_formated."</td>
              </tr></table>";
echo $cline;
echo "<table width='100%'><tr><td><b>". PSMSG ."</b><br>". draw_textarea_field('message', 'soft', '80', '5') ?>
<br><input type="submit" name="submit_button" value="<?php echo TEXT_SEND_EMAIL; ?>" class="button" /></td></tr></table>
 </form>
<?php } ?>
      </table>
    </td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php');